﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace LFSRecord2.View.Converters
{
    public class FileSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string[] abbs = { "B", "KB", "MB", "GB", "TB" };
            int level = 0;
            //double size = Double.Parse((string)value, CultureInfo.InvariantCulture);
            long valueLong = (long)value;
            double size = (double)valueLong;
            while (size > 1024)
            {
                size /= 1024;
                level++;
                if (level == 4)
                    break;
            }
            return size.ToString("#0.00", CultureInfo.InvariantCulture) + " " + abbs[level];
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return 0;
        }
    }
}
